package com.miracle.java2110;


import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @author lwc
 * @date 2021/11/5 17:40
 * @Description
 */
//七牛云上传图片的工具类
public class QiniuUtils2 {

    public static String accessKey="CVoPa3sTJHr9VSiTPCVlhfvLmhvuRrWDEp-Kl2Ak";
    public static String secretKey="5XPyX-pGfaSn97fiEYoAG4nCm47O5hXS1GDzyG3s";
    public static String bucket="javaee21100";

    //文件上传
    // 最简单的就是上传本地文件，直接指定文件的完整路径即可上传。
    // public static void uploadLoclFile2Qiniu(String localFilePath,String uploadFileName){
    //     Configuration cfg = new Configuration(Region.region2());
    //     UploadManager uploadManager = new UploadManager(cfg);
    //     Auth auth = Auth.create(accessKey, secretKey);
    //     String uploadToken = auth.uploadToken(bucket);
    //     Response response = null;
    //     try {
    //         response = uploadManager.put(localFilePath,uploadFileName,uploadToken);
    //
    //         DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
    //         System.out.println(putRet.key);
    //         System.out.println(putRet.hash);
    //
    //     } catch (QiniuException e) {
    //         Response r = e.response;
    //         System.err.println(r.toString());
    //         try {
    //             System.err.println(r.bodyString());
    //         } catch (QiniuException ex) {
    //
    //         }
    //     }
    // }
    //字节数组上传
    // 可以支持将内存中的字节数组上传到空间中。
    public static void uploadBytes2Qiniu(byte[] bytes,String uploadFileName){
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        Auth auth = Auth.create(accessKey, secretKey);
        String uploadToken = auth.uploadToken(bucket);
        Response response = null;

        try {
            response = uploadManager.put(bytes,uploadFileName,uploadToken);

            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet);
        } catch (QiniuException e) {
            Response r = e.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex) {

            }
        }
    }
    // public static void main(String[] args) throws IOException {
    //     // uploadLoclFile2Qiniu("D:\\ideaProject\\Day_springMVC\\ssm\\src\\main\\webapp\\image\\cat11.jpg","cat11.jpg");
    //
    //     byte[] bytes = FileUtils.readFileToByteArray(new File("D:\\\\ideaProject\\\\Day_springMVC\\\\ssm\\\\src\\\\main\\\\webapp\\\\image\\\\cat11.jpg"));
    //
    //     uploadBytes2Qiniu(bytes,"cat11.jpg");
    // }

}

